<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	/*if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}*/
	if(!empty($_RQ['mobile'])){
		$params['applyno']=$_RQ['applyno'];
		$return['params']['applyno']=$_RQ['applyno'];
	}
	//$fields=array('id','partnerlevel','partnertime','createtime','partnerstatus');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$merch=pdo_get('jy_jd_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['userid']),array('id','mid','merchname'));
		$v['merchname']=$merch['merchname'];
		$v['applytime']=date('Y/m/d H:i:s',$v['applytime']);
		$v['checktime']=date('Y/m/d H:i:s',$v['checktime']);
		
	}
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已结算','3'=>'已拒绝');
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'提现管理'),			
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'提现单号','type'=>'string'),
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'userid','title'=>'商户ID','type'=>'string'),
			array('name'=>'mid','title'=>'绑定会员ID','type'=>'string'),
			array('name'=>'realprice','title'=>'申请金额','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),				
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),	
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),	
		),
		'action'=>array(
			array('op'=>'check','title'=>'审核','type'=>'modal','show'=>array('status'=>0)),
			array('op'=>'orderinfo','title'=>'订单明细','type'=>'href'),
			array('type'=>'modal','op'=>'pay','title'=>'打款','show'=>array('status'=>1)),
		),
		),
		'search'=>array(
			
			array('name'=>'clearno','width'=>15,'title'=>'提现单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='check'){
	$id=intval($_RQ['id']);
	$return['status']=array(0=>'待审核',1=>'审核通过',2=>'已结算',3=>'拒绝');
	$return['item']=pdo_get('jy_jd_clearing',array('id'=>$id));
	$return['item']['pay_typetitle']=$return['item']['pay_type']?$this->pay_types[$return['item']['pay_type']]:'';
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
			array('name'=>'realprice','title'=>'申请金额','type'=>'string','read'=>true),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string','read'=>true),
			array('name'=>'pay_typetitle','title'=>'打款方式','type'=>'string','read'=>true),			
			array('type'=>'copy','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union'))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'))),
			array('type'=>'string','name'=>'pay_trade_no','title'=>'打款编号','show'=>array('pay_type'=>array('alipay','union'))),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'text','title'=>'申请事由','name'=>'content','read'=>true),
			)
	);
	$return['pay_types']=$this->pay_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	pdo_update('jy_jd_clearing',array('status'=>$post['status']),array('id'=>$post['id']));
	exi('审核成功','','list');
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$type=$post['type'];
		$status=intval($post['status']);
		if(!$status){
			exi('请先选择是否审核通过！','error');
		}
		if($status==1){
			$params=array('money'=>$post['finalprice'],'remark'=>'多商户提现');
			model('core/member')->pay_out(intval($post['mid']),$params);
			pdo_update('jy_jd_clearing',array('status'=>2,'paytime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			exi('打款成功！','','list');
		}else{
			if(!$post['remark']){
				exi('请填写审核不通过原因！','error');
			}
			pdo_update('jy_jd_clearing',array('status'=>3,'remark'=>trim($post['remark'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			exi('操作成功！','','list');
		}
	}
	$item=pdo_get('jy_jd_clearing',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$item['mid']=pdo_getcolumn('jy_jd_user',array('id'=>$item['userid'],'uniacid'=>$_SESSION['uniacid']),'mid');
	if($item['status']==2){
		exi('该提现已完成，请勿重复操作~','error');
	}
	$item['typetitle']=get_pay_types()['wechat'];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'打款',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，必须选择打款《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('type'=>'string','name'=>'finalprice','title'=>'打款金额','read'=>true),
			array('name'=>'typetitle','title'=>'打款方式','type'=>'string','read'=>true),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'string','show'=>array('status'=>2)),
			array('type'=>'text','title'=>'申请事由','name'=>'content','read'=>true),
		),
	);
	$return['statuss']=array(
		'1'=>'审核通过并打款',
		'2'=>'审核不通过',
	);
	$return['wxapps']=pdo_getall('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['mobile'])){
		$params['applyno']=$_RQ['applyno'];
		$return['params']['applyno']=$_RQ['applyno'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_jd_clearing',$params);
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已结算','3'=>'已拒绝');
	if($list){
		foreach($list as &$item){
			$merch=pdo_get('jy_jd_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['userid']),array('id','mid','merchname'));
			$item['merchname']=$merch['merchname'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['status']=$return['status'][$item['status']];
			$item['applytime']=date('Y/m/d H:i:s',$item['applytime']);
			$item['checktime']=date('Y/m/d H:i:s',$item['checktime']);
		}
	}	
	$header=array(
		'clearno'=>'提现单号',
		'merchname'=>'商户名称',
		'userid'=>'商户ID',
		'realprice'=>'申请金额',
		'realpricerate'=>'抽成后金额',
		'finalprice'=>'最终打款',
		'status'=>'状态',
		'createtime'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,'提现日志');
}
if($_RQ['op']=='orderinfo'){
	if($_RQ['id']){
		$id=intval($_RQ['id']);
		$this->session('id',$id);
	}else{
		$id=$this->session('id');
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'clearstatus'=>1,'clearid'=>$id);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
    //$fields=array('id','order','name','intime','hotelid','roomid','ispay','paytype','fee','mid','createtime','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_get('jy_jd_room',array('id'=>$v['roomid']));
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
			array('op'=>'dai','title'=>'第三方报单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('name'=>'num','title'=>'房间数量','type'=>'string'),
			array('type'=>'img_string','title'=>'入住人',
			'data'=>array(
				array('title'=>'入住人：','name'=>'name'),
				array('title'=>'入住电话：','name'=>'mobile')
				)
			),
			array('type'=>'img_string','title'=>'入住时间',
			'data'=>array(
				array('title'=>'入住时间：','name'=>'intime'),
				array('title'=>'离店时间：','name'=>'endtime')
				)
			),
			array('type'=>'img_string','title'=>'费用',
			'data'=>array(
				array('title'=>'订单金额：','name'=>'fee'),
				array('title'=>'包含押金：','name'=>'yafee')
				)
			),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'dai_type','title'=>'预定平台','type'=>'radio','options'=>'dai_type'),			
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),	
		),
		'action'=>array(
			array('type'=>'post','op'=>'tuifang','title'=>'退房','show'=>array('status'=>6)),
			array('type'=>'modal','op'=>'item','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'用户昵称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['status']=$this->order_status;
	$return['paytype']=get_pay_types();
	$return['ispay']=array('0'=>'未支付','1'=>'已支付');
	$return['dai_type']=$this->dai_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='item'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('id'=>$id));
	if(!$item){
		exi('指定订单不存在~','error');
	}
	$item['hotel']=pdo_get('jy_jd_hotel',array('id'=>$item['hotelid']));
	$item['room']=pdo_get('jy_jd_room',array('id'=>$item['roomid']));
	$item['statustitle']=$this->order_status[$item['status']];
	$endtime=strtotime($item['intime'])+$item['num']*24*3600;
	$item['endtime']=date('Y-m-d',$endtime);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'string'),
			array('title'=>'离店时间：','name'=>'endtime','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
			array('title'=>'订单押金：','name'=>'yafee','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),
		),
	);
	$return['status']=$this->order_status;
	$return['paytype']=get_pay_types();
	$return['ispay']=array('0'=>'未支付','1'=>'已支付');
	$return['dai_type']=$this->dai_type;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_jd_clearing',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_jd_clearing',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>